<?php
class v1_transform{
    function index(){
    	$data['basic']=$this->yum->config['Manage']['domain'].'?c=transform&a=index';
		$params=array('limit'=>array(intval($this->yum->get('start')),15));
		if($this->yum->get('order',true)){
			$data['order']=$this->yum->get('order',true);$data['rule']=$this->yum->get('rule',true);
			if(in_array($data['order'],array('money','commision','time','date'))&&in_array($data['rule'],array('desc','asc'))){
				$params['order'][$data['order']]=$data['rule'];
			}
		}else{
			$data['order']='date';$data['rule']='desc';
			$params['order'][$data['order']]=$data['rule'];
		}
		if($this->yum->get('date',true)){
			$data['date']=explode(',',$this->yum->get('date',true));
			if(count($data['date'])==2){
				$params['where']=array('date >='=>strtotime($data['date'][0]),'date <='=>strtotime($data['date'][1]));
				$data['basic'].='&date='.$data['date'][0].','.$data['date'][1];
			}
		}
		if($this->yum->get('locked',true)){
			$temp=$this->yum->get('locked',true);
			if(is_array($temp)){
				foreach($temp as $key=>$value){
					$data['locked'][$value]=intval($value);
					$data['basic'].='&locked[]='.$value;
				}
				$params['where']['locked IN']=$data['locked'];
			}
		}
		if($this->yum->get('search',true)){
			$data['search']=urldecode($this->yum->get('search',true));$data['tab']=$this->yum->get('tab',true);
			if($data['search']!=''){
				if($data['tab']=='order'){
					$params['where']['`order` LIKE']='%'.$data['search'].'%';
				}else if($data['tab']=='mail'){
					$params['where']['member LIKE']='%'.$data['search'].'%';
				}
				$data['basic'].='&tab='.$data['tab'];
				$data['basic'].='&search='.urlencode($data['search']);
			}
		}
    	$transform=$this->yum->model('data');
    	$data['total']=$transform->transform(array_merge($params,array('select'=>'COUNT(*) AS count','limit'=>array())));
    	$data['total']=reset($data['total']);$data['total']=$data['total']['count'];
		$data['transform']=$transform->transform($params);
		$pagination=$this->yum->library('pagination',array('query_string'=>'start','base_url'=>$data['basic'].'&order='.$data['order'].'&rule='.$data['rule'],'total_rows'=>$data['total'],'per_page'=>15,'first_link'=>'首页','last_link'=>'末页','cur_tag_open'=>'<a href="#" class="active">','cur_tag_close'=>'</a>'));
		$data['pagination']=$pagination->create_links();
		$this->yum->set($data);
		$this->yum->view('index.php');
    }
    function detail(){
		$order=$this->yum->get('order',true);
		$transform=$this->yum->model('data');
		$data['order']=$transform->select('transform',array('where'=>array('`order` ='=>$order)));
		$this->yum->set($data);
		$this->yum->view('detail.php');
    }
    function locked(){
		$output=array('result'=>0,'msg'=>'','offset'=>0,'data'=>array());
		$order=$this->yum->get('order');$locked=intval($this->yum->get('locked',true));
		$params=array('set'=>array('locked='=>$locked));
		if(is_array($order)){
			$params['where']=array('`order` IN'=>$order);
		}elseif(is_string($order)){
			$params['where']=array('`order`='=>intval($order));
		}else{
			exit();
		}
		$this->yum->library('mysql')->startTrans();
		$transform=$this->yum->model('data');
		$temp=$transform->select('transform',array('select'=>'member,`order`,SUM(commision) AS commision,locked','where'=>$params['where'],'group'=>array('order')));
		if(!empty($temp)){
			$information=$this->yum->model('information');
			$log=$this->yum->model('log');
			$session=$this->yum->library('session');
			foreach($temp as $value){
				if(($value['locked']==1&&$locked==0)||($value['locked']==2&&$locked==0)){
					$information->UMation(array('where'=>array('member='=>$value['member']),'set'=>array('money=money+'=>$value['commision'])));
					$mation=$information->GMation(array('where'=>array('member='=>$value['member'])));
					$mation=reset($mation);
					$insert['field']=array('member','label','date','ip','content');
					$insert['value'][]=array($value['member'],'payment.detail',$this->yum->runtime,$_SERVER["REMOTE_ADDR"],serialize(array('money'=>$value['commision'],'balance'=>$mation['money'],'remark'=>('订单:'.$value['order'].'确认支付.'))));
					$insert['value'][]=array($session->GVAR('member'),'transform',$this->yum->runtime,$_SERVER["REMOTE_ADDR"],'审核订单:'.$value['order'].'确认');
					$log->ILog($insert);
				}elseif($value['locked']==0&&$locked==2){
					$information->UMation(array('where'=>array('member='=>$value['member']),'set'=>array('money=money-'=>$value['commision'])));
					$mation=$information->GMation(array('where'=>array('member='=>$value['member'])));
					$mation=reset($mation);
					$insert['field']=array('member','label','date','ip','content');
					$insert['value'][]=array($value['member'],'payment.detail',$this->yum->runtime,$_SERVER["REMOTE_ADDR"],serialize(array('money'=>-$value['commision'],'balance'=>$mation['money'],'remark'=>('订单:'.$value['order'].'无效扣费.'))));
					$insert['value'][]=array($session->GVAR('member'),'transform',$this->yum->runtime,$_SERVER["REMOTE_ADDR"],'审核订单:'.$value['order'].'无效扣费');
					$log->ILog($insert);
				}elseif($value['locked']==1&&$locked==2){
					$log->ILog(array('member'=>$session->GVAR('member'),'label'=>'transform','date'=>$this->yum->runtime,'ip'=>$_SERVER["REMOTE_ADDR"],'content'=>'审核订单:'.$value['order'].'无效.'));
				}
			}
		}
		$affected=$transform->update('transform',$params);
		if(is_array($order)){
			if($affected==0){
				$output['msg']='重复的数据提交.';
				$this->yum->library('mysql')->rollback();
			}else{
				$output['result']=2;
				$this->yum->library('mysql')->commit();
			}
		}elseif($affected!==false){
			$output['result']=5;
			$output['td']=array('offset'=>9,'data'=>array($locked==0?'确认':'无效'));
			$output['exec']=array('attr'=>array('mixed'=>'sub:{locked:'.($locked==0?2:0).'}'),'text'=>($locked==0?'无效':'确认'));
			$this->yum->library('mysql')->commit();
		}else{
			$output['msg']='请求有误.';
			$this->yum->library('mysql')->rollback();
		}
		header('Content-Type:text/plain');
		echo json_encode($output);
    }
}
?>